from sc_engine.apps import messages as msgs
from sc_engine.scrape.service import get_map_info

def make_app(app_name, root_config, config):
    lookup = get_map_info
    app = FetchMapApp(lookup)

    return {
        msgs.MapFetchRequested : app.map_fetch_requested,
    }

class FetchMapApp(object):
    """Fetch information on maps from the kespa website."""
    def __init__(self, lookup):
        self.lookup = lookup

    def map_fetch_requested(self, msg):
        result = self.lookup(msg.map_id)

        if result:
            return msgs.MapFetchAnnouncement(msg.map_id,
                result['name'],
                result['version'],
                result['width'],
                result['height'],
                result['image_filename'])
        else:
            return msgs.MapFetchNonExistantMap(msg.map_id)

